clear all
set more off


local J = `1'

local N = `2'



use flexlogit
bsample, cluster(ID) idcluster(newID)
drop ID
rename newID ID

*limited z variation method
*tercile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(3)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha1_`i' = _b[x`i']
 }
restore

drop zdecile stdz



*quartile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(4)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha2_`i' = _b[x`i']
 }

restore

drop zdecile stdz

*decile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(10)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha3_`i' = _b[x`i']
 }

restore

drop zdecile stdz


*ventile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(20)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha4_`i' = _b[x`i']
 }

restore

drop zdecile stdz

*Percentile
preserve 
bysort ID:egen stdz=sd(z)
keep ID stdz
duplicates drop
xtile zdecile=stdz,nq(100)
tempfile zdecile 
save `zdecile',replace
restore 

merge m:1 ID using `zdecile'
drop _merge

preserve
 keep if zdecile==1 
 clogit chosen x* z ,group(ID) 
 
 forvalues i=1/3 {
 local alpha5_`i' = _b[x`i']
 }

restore

drop zdecile stdz


keep if _n == 1
keep chosen

forvalues j=1/5{
 forvalues i=1/3{
 gen alpha`j'_`i'=`alpha`j'_`i''
 }
}
drop chosen







